import 'dart:async';
import 'dart:io';

import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/wroks/search_bean.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/bean/user.dart';
import 'package:app/src/http/bean/ywx_count.dart';
import 'package:app/src/http/bean/ywx_limit.dart';
import 'package:app/src/http/sign.dart';
import 'package:app/src/http/url.dart';
import 'package:app/src/manager/user_manager.dart';
import 'package:app/src/manager/ywx_manager.dart';
import 'package:app/src/ui/dialog/dailog.dart';

import '../bean/worksdo/material_requisition_bean.dart';

///审核提交
class SubmitModel {
  ///审核提交
  ///Status 审核状态(同意，退回，终止)
  ///Intro 审核意见
  ///pics 图片地址（多张图片用|分隔）
  ///subData 审核提交数据
  static Future<AppHttpResult<String, String>> contractapply(
      BuildContext context, bool isEnableLoad, String Status, String Intro,
      {String? pics = null, SubData? subData = null}) async {
    Completer completer = new Completer<AppHttpResult<String, String>>();
    var url = Url.baseurl() + Url.contractapply;
    var token = AppDataConfig.getToken();
    if (subData == null) {
      subData = SubData();
    }
    subData.commondata = CommonData();
    subData.ReviewType =
        AuditManager.getAuditManager().item!.ReviewType!; //审核类型
    subData.businessdata = AuditManager.getAuditManager().ReviewTypeId; //审核类型ID
    subData.BodyId = AuditManager.getAuditManager().BodyId; //采购合同审核需要。
    subData.YwxId = AuditManager.getAuditManager().YwxId;
    subData.YwxName = AuditManager.getAuditManager().YwxName;
    subData.Status = Status; //审核状态
    subData.IsAgree = Status; //也是审核状态
    subData.Intro = Intro; //审核意见
    // print(
    //     "========================================================入库仓位2：${subData.FLoseApplyInfo?.Storehouse}");
    // print(
    //     "========================================================检验员2：${subData.FLoseApplyInfo?.Checker}");
    if (pics != null) {
      subData.pics = pics; //图片地址（多张图片用|分隔）
    }
    var queryParameters = Map<String, dynamic>();
    var data = subData.jsonEncodeToJson();
    queryParameters['data'] = data;
    // print('提交数据：${data}');
    // return Future.value(null);
    // return Future.value(null);
    var sign = getSign(queryParameters);
    queryParameters['sign'] = sign;
    var isEnableLog = AppDataConfig.isEnableLog;
    //isEnableLog=true;
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        //print('错误:\t$code\t$error');
        AppDialog.showDialogWithError(context, code, error);
      })
      // ..onSuccess((result) {
      //   print('成功:\n$result');
      // })
      // ..onFinish(() {
      //   print('结束');
      // })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormDatas(queryParameters)
      ..contentType = ContentType.form
      ..isEnableLog = isEnableLog
      ..postASync<AppHttpResult<String, String>>(AppHttpResult()
            ..Data = ""
            ..t2 = "")
          ?.then((result) {
        completer.complete(result);
      });
    return completer.future as Future<AppHttpResult<String, String>>;
  }

  ///导出单据；导出的单据文件是实时生产的，每次都不一样。
  ///Type 单据类型：NC ,金锐
  ///subData 审核提交数据
  static Future<AppHttpResult<String, String>> exportDocuments(
      BuildContext context, bool isEnableLoad, String Type,
      {SubData? subData = null}) async {
    Completer completer = new Completer<AppHttpResult<String, String>>();
    var url = Url.baseurl() + Url.documentexport;
    var token = AppDataConfig.getToken();
    if (subData == null) {
      subData = SubData();
    }
    subData.commondata = CommonData();
    subData.ReviewType =
        AuditManager.getAuditManager().item!.ReviewType!; //审核类型
    subData.businessdata = AuditManager.getAuditManager().ReviewTypeId; //审核类型ID
    subData.Type = Type; //导出类型
    var queryParameters = Map<String, dynamic>();
    queryParameters['data'] = subData.jsonEncodeToJson();
    //print('提交数据：${ subData.jsonEncodeToJson()}');
    var sign = getSign(queryParameters);
    queryParameters['sign'] = sign;
    var isEnableLog = AppDataConfig.isEnableLog;
    //isEnableLog=true;
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        // print('错误:\t$code\t$error');
        AppDialog.showDialogWithError(context, code, error);
      })
      // ..onSuccess((result) {
      //   print('成功:\n$result');
      // })
      // ..onFinish(() {
      //   print('结束');
      // })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormDatas(queryParameters)
      ..contentType = ContentType.form
      ..isEnableLog = isEnableLog
      ..postASync<AppHttpResult<String, String>>(AppHttpResult()
            ..Data = ""
            ..t2 = "")
          ?.then((result) {
        completer.complete(result);
      });
    return completer.future as Future<AppHttpResult<String, String>>;
  }
}
